#include "pwm.h"
#include "sys.h"
TIM_ICInitTypeDef  TIM1_ICInitStructure;
u16 RX_CH[10];
void pwm_oc_init()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);	//使能TIM2时钟
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //使能GPIOB时钟
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_8;  //PA0 清除之前设置  
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PA0 输入  
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOA,GPIO_Pin_8);						 //PA0 下拉
//	GPIO_ResetBits(GPIOA,GPIO_Pin_9);						 //PA0 下拉
//	GPIO_ResetBits(GPIOA,GPIO_Pin_10);						 //PA0 下拉
//	GPIO_ResetBits(GPIOA,GPIO_Pin_11);						 //PA0 下拉
	
	//初始化定时器2 TIM2	 
	TIM_TimeBaseStructure.TIM_Period = 0xffff; //设定计数器自动重装值 
	TIM_TimeBaseStructure.TIM_Prescaler =72-1; 	//预分频器   
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  
		//初始化TIM1输入捕获参数
  	TIM1_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 	选择输入端 IC1映射到TI1上
  	TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising  ;	//上升沿捕获
  	TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
  	TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;	 //配置输入分频,不分频 
  	TIM1_ICInitStructure.TIM_ICFilter = 0x0;//IC1F=0000 配置输入滤波器 不滤波
  	TIM_ICInit(TIM1, &TIM1_ICInitStructure);
		
//		TIM1_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01 	选择输入端 IC1映射到TI1上
//  	TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;	//上升沿捕获
//  	TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
//  	TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;	 //配置输入分频,不分频 
//  	TIM1_ICInitStructure.TIM_ICFilter = 0x8;//IC1F=0000 配置输入滤波器 不滤波
//  	TIM_ICInit(TIM1, &TIM1_ICInitStructure);
//	//初始化TIM输入捕获参数
//  	TIM1_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 	选择输入端 IC1映射到TI1上
//  	TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;	//上升沿捕获
//  	TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
//  	TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;	 //配置输入分频,不分频 
//  	TIM1_ICInitStructure.TIM_ICFilter = 0x8;//IC1F=0000 配置输入滤波器 不滤波
//  	TIM_ICInit(TIM1, &TIM1_ICInitStructure);
		
	//中断分组初始化
	NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;  //TIM2中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级2级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //从优先级0级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 
	
	TIM_ITConfig(TIM1,TIM_IT_CC1,ENABLE);//允许更新中断 ,允许CC1IE捕获中断	
//	TIM_ITConfig(TIM1,TIM_IT_CC2,ENABLE);//允许更新中断 ,允许CC1IE捕获中断		
//	TIM_ITConfig(TIM1,TIM_IT_CC3,ENABLE);//允许更新中断 ,允许CC1IE捕获中断	
    TIM_Cmd(TIM1,ENABLE ); 	//使能定时器2
 }
void oc_stop(void)
{
   TIM_ITConfig(TIM1,TIM_IT_CC1,DISABLE);//允许更新中断 ,允许CC1IE捕获中断	
   TIM_Cmd(TIM1,DISABLE ); 	//使能定时器2
}
void oc_start(void)
{
		
   TIM_ITConfig(TIM1,TIM_IT_CC1,ENABLE);//允许更新中断 ,允许CC1IE捕获中断	
   TIM_Cmd(TIM1,ENABLE ); 	//使能定时器2 
}
void ppm_out_stop(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_4;  //PA0 清除之前设置  
	GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU; //PA0 输入  
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
		TIM_ITConfig(  //使能或者失能指定的TIM中断
		TIM3, //TIM3
		TIM_IT_Update ,
		DISABLE  //使能
		);
    TIM_Cmd(TIM3, DISABLE);  //使能TIMx外设
}

void ppm_out_start(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_4;  //PA0 清除之前设置  
	GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP; //PA0 输入  
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	TIM_ITConfig(  //使能或者失能指定的TIM中断
		TIM3, //TIM3
		TIM_IT_Update ,
		ENABLE  //使能
		);
    TIM_Cmd(TIM3, ENABLE);  //使能TIMx外设
}
 void ppm_out()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);	//使能TIM3时钟
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  //使能GPIOB时钟
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_4;  //PA0 清除之前设置  
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //PA0 输入  
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOB,GPIO_Pin_4);						 //PA0 下拉
	
	TIM_TimeBaseStructure.TIM_Period = 20000-1; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 
	TIM_TimeBaseStructure.TIM_Prescaler =72-1; //设置用来作为TIMx时钟频率除数的预分频值  1Mhz的计数频率  
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
 
//	TIM_ITConfig(  //使能或者失能指定的TIM中断
//		TIM3, //TIM3
//		TIM_IT_Update ,
//		ENABLE  //使能
//		);
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

//	TIM_Cmd(TIM3, ENABLE);  //使能TIMx外设
 }
u8 ppm_out_state=0;//ppm状态机
u16 time_cnt=0;//ppm状态机
u16 TX_CH[8]={1800,1000,1000,1000,1000,1000,1000,1000};
void TIM3_IRQHandler(void)   //TIM3中断
{
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
		{
		   TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //清除TIMx的中断待处理位:TIM 中断源 
			switch (ppm_out_state)
			{
				case 0://起始低电平
					GPIO_ResetBits(GPIOB,GPIO_Pin_4);
					TIM3->ARR=400;//0.4ms低电平
					time_cnt+=400;
					ppm_out_state=1;
					break;
				case 1://1通道高电平
					GPIO_SetBits(GPIOB,GPIO_Pin_4);	//拉高电平
					TIM3->ARR=TX_CH[0]-400;//高电平时间
					time_cnt+=TX_CH[0]-400;
					ppm_out_state=2;
					break;
				case 2://1通道低电平
					GPIO_ResetBits(GPIOB,GPIO_Pin_4);
					TIM3->ARR=400;//0.4ms低电平
					time_cnt+=400;
					ppm_out_state=3;
					break;
				case 3://2通道高电平
					GPIO_SetBits(GPIOB,GPIO_Pin_4);	//拉高电平
					TIM3->ARR=TX_CH[1]-400;//高电平时间
					time_cnt+=TX_CH[1]-400;
					ppm_out_state=4;
					break;
				case 4://2通道低电平
					GPIO_ResetBits(GPIOB,GPIO_Pin_4);
					TIM3->ARR=400;//0.4ms低电平
					time_cnt+=400;
					ppm_out_state=5;
					break;
				case 5://3通道高电平
					GPIO_SetBits(GPIOB,GPIO_Pin_4);	//拉高电平
					TIM3->ARR=TX_CH[2]-400;//高电平时间
					time_cnt+=TX_CH[2]-400;
					ppm_out_state=6;
					break;
				case 6://3通道低电平
					GPIO_ResetBits(GPIOB,GPIO_Pin_4);
					TIM3->ARR=400;//0.4ms低电平
					time_cnt+=400;
					ppm_out_state=7;
					break;
				case 7://4通道高电平
					GPIO_SetBits(GPIOB,GPIO_Pin_4);	//拉高电平
					TIM3->ARR=TX_CH[3]-400;//高电平时间
					time_cnt+=TX_CH[3]-400;
					ppm_out_state=8;
					break;
				case 8://4通道低电平
					GPIO_ResetBits(GPIOB,GPIO_Pin_4);
					TIM3->ARR=400;//0.4ms低电平
					time_cnt+=400;
					ppm_out_state=9;
					break;
				case 9://5通道高电平
					GPIO_SetBits(GPIOB,GPIO_Pin_4);	//拉高电平
					TIM3->ARR=TX_CH[4]-400;//高电平时间
					time_cnt+=TX_CH[4]-400;
					ppm_out_state=10;
					break;
				case 10://5通道低电平
					GPIO_ResetBits(GPIOB,GPIO_Pin_4);
					TIM3->ARR=400;//0.4ms低电平
					time_cnt+=400;
					ppm_out_state=11;
					break;
				case 11://6通道高电平
					GPIO_SetBits(GPIOB,GPIO_Pin_4);	//拉高电平
					TIM3->ARR=TX_CH[5]-400;//高电平时间
					time_cnt+=TX_CH[5]-400;
					ppm_out_state=12;
					break;
				case 12://6通道低电平
					GPIO_ResetBits(GPIOB,GPIO_Pin_4);
					TIM3->ARR=400;//0.4ms低电平
					time_cnt+=400;
					ppm_out_state=13;
					break;
				case 13://7通道高电平
					GPIO_SetBits(GPIOB,GPIO_Pin_4);	//拉高电平
					TIM3->ARR=TX_CH[6]-400;//高电平时间
					time_cnt+=TX_CH[6]-400;
					ppm_out_state=14;
					break;
				case 14://7通道低电平
					GPIO_ResetBits(GPIOB,GPIO_Pin_4);
					TIM3->ARR=400;//0.4ms低电平
					time_cnt+=400;
					ppm_out_state=15;
					break;
				case 15://8通道高电平
					GPIO_SetBits(GPIOB,GPIO_Pin_4);	//拉高电平
					TIM3->ARR=TX_CH[7]-400;//高电平时间
					time_cnt+=TX_CH[7]-400;
					ppm_out_state=16;
					break;
				case 16://8通道低电平
					GPIO_ResetBits(GPIOB,GPIO_Pin_4);
					TIM3->ARR=400;//0.4ms低电平
					time_cnt+=400;
					ppm_out_state=17;
					break;
				case 17://1通道高电平
					GPIO_SetBits(GPIOB,GPIO_Pin_4);	//拉高电平
					TIM3->ARR=20000-time_cnt;//高电平时间20ms-总时间
					time_cnt=0;
					ppm_out_state=0;
					break;	
				default:
					break;
			}
			
		}
}



u8 rc_flag=0;
 static u16 temp_cnt1;
 u8  TIM1CH1_CAPTURE_STA=0;	//输入捕获状态		
 void TIM1_CC_IRQHandler(void)
 {

	 	if(TIM1->SR & TIM_IT_CC1) 
	{
		TIM1->SR = ~TIM_IT_CC1;//TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
		TIM1->SR = ~TIM_FLAG_CC1OF;
		if(GPIOA->IDR & GPIO_Pin_8)
		{
			temp_cnt1 = TIM_GetCapture1(TIM1);
			TIM1->CNT=0;
			if(temp_cnt1>2800)
			rc_flag=1;//一帧开头
			if(rc_flag!=0&&temp_cnt1<2200)
			{
				rc_flag++;
				//TIM3->CCR2=temp_cnt1+3500;
			}
			if(rc_flag==2)
			   RX_CH[0]=temp_cnt1;
			
			if(rc_flag==3)
			 RX_CH[1]=temp_cnt1;
			
			if(rc_flag==4)
			 RX_CH[2]=temp_cnt1;
			
			if(rc_flag==5)
			 RX_CH[3]=temp_cnt1;
			
			if(rc_flag==6)
			 RX_CH[4]=temp_cnt1;
			
			if(rc_flag==7)
			 RX_CH[5]=temp_cnt1;
			
			if(rc_flag==8)
			 RX_CH[6]=temp_cnt1;
			
			if(rc_flag==9)
			  RX_CH[7]=temp_cnt1;
	    //TIM3->CCR2=3500+Rc_Pwm_In[2];
		}
	}
	 
} 
	 
	 
	 
	 
	 
	 
	 
	 
	
		/*	if(TIM1->SR & TIM_IT_CC1) 
			{
				TIM1->SR = ~TIM_IT_CC1;//TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
				TIM1->SR = ~TIM_FLAG_CC1OF;
				
				if(TIM1CH1_CAPTURE_STA==0)//判断为上升沿
				{
					TIM1CH1_CAPTURE_STA=1;
					temp_cnt1 = TIM_GetCapture1(TIM1);
					TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Falling);		//设置为下降沿捕获
				}
				else//判断为下降升沿
				{
					TIM1CH1_CAPTURE_STA=0;
					temp_cnt1_2 = TIM_GetCapture1(TIM1);
					TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Rising);		//设置为上升降沿捕获
						if(temp_cnt1_2>=temp_cnt1)
						RX_CH[0] = temp_cnt1_2-temp_cnt1-25;
					else
						 RX_CH[0] = 0xffff-temp_cnt1+temp_cnt1_2+1-25;
				}

			}*/
				
 